import Api from "@Api";
import { GET } from "@Router";
import User from "@app/db/User";
export default class Example extends Api {
  // 接口的地址为 /api/example/index
  @GET
  public async index() {
    this.success('data', '成功')
  }

  // 接口的地址为 /api/example/say_hi
  @GET
  public async sayHi() {
    this.notAuth()
  }
  // 调用数据库 添加
  @GET
  public async add() {
    const query: any = this.getQuery()
    const res = await User.add(query)
    this.success(res)
  }
  // 调用数据库 删除
  @GET
  public async del() {
    const query: any = this.getQuery()
    await User.del(query.id)
    this.success()
  }
  // 调用数据库 修改
  @GET
  public async update() {
    const query = this.getQuery()
    await User.update(query)
    this.success()
  }
  // 调用数据库 按id查询
  @GET
  public async get() {
    const query = this.getQuery()
    const res = await User.get(query.id)
    this.success(res)
  }
  // 调用数据库 查询满足条件的所有记录
  @GET
  public async where() {
    const query = this.getQuery()
    const res = await User.where(query)
    this.success(res)
  }
  // 调用数据库 自定义操作，
  @GET
  public async custom() {
    const query = this.getQuery()
    const res: any = []
    // 数据中包含某个值
    await User.custom((list) => list.forEach(e => e.name && e.name.includes(query.name) && res.push(e)))
    this.success(res)
  }

}
